<?php
namespace Home\Controller;
// use Think\Controller;
// use Home\Controller\CommonController;
include_once("Common.php");
include_once("mysqlha.php");
include_once("UpFileTool.class.php");
// 用于显示主页面
class TuangouController extends CommonController
{
    
	//显示拼团专区
	public function showList(){
		
		$page = $_REQUEST["pageNum"];
		$pagecount = $_REQUEST['numPerPage'];
		$keywords = $_REQUEST['keywords'];
		if($page == null||$page<=1){
			$page=1;
		}

		if($pagecount == null||$pagecount<=0){
			$pagecount=20;
		}
		$Model=M();
		$startcount = ($page-1)*$pagecount;
		if($keywords == "") {
			$result = $Model->Query("select * from buygroup order by sort  limit ".$startcount.",".$pagecount."");
			$count = $Model->Query("select count(*) as count FROM buygroup");
		}else{
		}
		$param = array(
			"count"=>$count[0]['count'],
			"numPerPage"=> $pagecount,
			"pageNum"=>$page
		);
		$this->assign('param',$param);
		$this->assign('resultList',$result);
		$this->display("Admin/buygroupList.php");
	}
	
	//修改分类页面
    public function changeOne(){
        $Model = M();
        $id = $_REQUEST['id'];
		$Obj = $Model->Query("select * from buygroup where buygroup_id=".$id."");
		$this->assign('Obj',$Obj[0]);
        $this->display('Admin/changbuyGroup.php');
    }

    //修改分类
    public function OneChange(){
        $name = $_REQUEST['name'];
        $buygroup_id = $_REQUEST['id'];
		$sort = $_REQUEST['sort'];
		$mcount = $_REQUEST['mcount'];
		$money = $_REQUEST['money'];
		$yunfare = $_REQUEST['yunfare'];
        
        $updateArr = array();
		$updateArr['name'] = $name;
		$updateArr['sort'] = $sort;
		$updateArr['money'] = $money;
		$updateArr['mcount'] = $mcount;
		$updateArr['yunfare'] = $yunfare;
        if ($_FILES) {

            $UpFileTool = new \Think\UpFileTool('images');// 实例化上传类

            $UpFileTool->upImage(0.3,'Uploads','Uploads');
            $images=$UpFileTool->srcImage;
            $sumbimages = $UpFileTool->sumbImage;
            if($images!=""){
				$updateArr['image'] = $images;
				$updateArr['sumbimage'] = $sumbimages;
			}
        }
		update('buygroup',$updateArr,"buygroup_id=".$buygroup_id."");
		$returnArr = array(
			"code"=>200,
			"callbackType"=>"closeCurrent",
			"navTabId"=>"buygroup_list",
			"forwardUrl"=>"index.php?c=Tuangou&a=showList",
			"msg"=>"插入成功"
		);
		echo json_encode($returnArr);
    }
	
	public function ChangeSort(){
		$sort = $_REQUEST['sort'];
		$id = $_REQUEST['id'];

		$updateArr = array(
		    "sort"=>$sort
		);
		update('buygroup',$updateArr,"buygroup_id=".$id."");
		$returnArr = array(
			"code"=>200,
			"forwardUrl"=>'index.php?c=Tuangou&a=showList',
			"msg"=>"更新成功"
		);
		echo json_encode($returnArr);

	}
	
	//获取金钱详情
	public function getMD($money){
		$result = "";
		if($money>=0&&$money<100){
			$result=$money."件";
		}else if($money>=100&&$money<1000){
			$b = (int)$money/100;
			$result=$b."百件";
		}else if($money>=1000&&$money<10000){
			$b = (int)$money/1000;
			$result=$b."千件";
		}else if($money>=10000){
			$b = (int)$money/10000;
			$result=$b."万件";
		}
		return $result;
	}
	
	//获取广告图的商品
	public function getPintuanAd(){
		$classifys = $_REQUEST['classifys'];
		$buygroup_id = $_REQUEST['buygroup_id'];
		$Model = M();
		$page = $_REQUEST['page'];
		$pagecount = $_REQUEST['pagecount'];
		
		if($page == null||$page<=0){
			$page=0;
		}

		if($pagecount == null||$pagecount<=0){
			$pagecount=6;
		}
		$startcount = $page*$pagecount;
		$onegroup = $Model->Query("select * from buygroup where buygroup_id=".$buygroup_id."");
		$result = $Model->Query("select tuancount,title,images,goodsid from goods where buygroup_id=".$buygroup_id." and istuangou=1 and xilie in(".$classifys.")  order by addtime desc limit ".$startcount.",".$pagecount."");
		for($i=0;$i<sizeof($result);$i++){
			$result[$i]['mcount'] = $onegroup[0]['mcount'];
			$result[$i]['money'] = $onegroup[0]['money'];
			$result[$i]['yunfare'] = $onegroup[0]['yunfare'];
			$result[$i]['tuancount'] = $this->getMD($result[$i]['tuancount']);
			$result[$i]['images'] = explode(',',$result[$i]['images']); 
			$result[$i]['images'] = $result[$i]['images'][0];
		}
		$resultArr = array(
		    "code"=>200,
			"result"=>$result
		);
    	echo json_encode($resultArr);
	}
  
	
	//获取拼团专区的商品
	public function getPinTuangroup(){
		$buygroup_id = $_REQUEST['buygroup_id'];
		$Model = M();
		$page = $_REQUEST['page'];
		$pagecount = $_REQUEST['pagecount'];
		$user_id = $_REQUEST['user_id'];
		
		if($page == null||$page<=0){
			$page = 0;
		}

		if($pagecount == null||$pagecount<=0){
			$pagecount = 6;
		}

		$startcount = $page*$pagecount;
		$onegroup = $Model->Query("select * from buygroup where buygroup_id=".$buygroup_id."");
		$adlist = $Model->Query("select * from buygroupad where buygroup_id=".$buygroup_id." order by sort");
		$result_a = $Model->Query("select tuancount,title,images,goodsid from goods where buygroup_id=".$buygroup_id." and istuangou = 1 order by tuancount desc limit ".$startcount.",".$pagecount."");
		// var_dump($result_a);die;
		//判断拼团id，获取activity表特定商品类型
		switch ($buygroup_id) {
			case 1:
				$type = 7;
				break;
			case 2:
				$type = 9;
				break;
			case 3:
				$type = 9;
				break;
		}
		//end
		
		$goodsid = D('Activity')->lists(array(
										"field" => "goodsid",
										"where" => array("type" => $type)
										));

		$goodsid = $this->arrCstr($goodsid, 'goodsid');

		$result_b = D('Goods')->lists(array(
										"field"      => "tuancount,title,images,goodsid",
										"where_a"    => array("buygroup_id" => $buygroup_id, "istuangou"=>1),
										"where_b"	 => "goodsid IN (" . $goodsid . ")",
										"order"      => "tuancount desc",
										"startcount" => $startcount,
										"pagecount"  => $pagecount
									));
		if($result_b) {
			// echo '1';
			$result = array_merge($result_b, $result_a);
		} else {
			$result = $result_a;
		}
		// var_dump($result);die;
		for($i=0;$i<sizeof($result);$i++){
			$result[$i]['mcount'] = $onegroup[0]['mcount'];
			$result[$i]['money'] = $onegroup[0]['money'];
			$result[$i]['yunfare'] = $onegroup[0]['yunfare'];
			$result[$i]['tuancount'] = $this->getMD($result[$i]['tuancount']);
			$result[$i]['images'] = explode(',',$result[$i]['images']); 
			$result[$i]['images'] = $result[$i]['images'][0];
		}

		$resultArr = array(
		    "code"=>200,
			"result"=>$result,
			"adlist"=>$adlist 
		);

    	echo json_encode($resultArr);
	}
	
	//获取拼团专区列表
	public function getBuyGroup(){
		$Model = M();
		$result = $Model->Query("select * from buygroup");
		$user = array($result);
		echo getSuccessJson($user);
	}

    //添加拼团专区
    public function showaddOne(){
        $this->display('Admin/addbuyGroup.php');
    }

    //添加拼团专区
    public function addOne(){
        $mcount = $_REQUEST['mcount'];
		$money = $_REQUEST['money'];
		$name = $_REQUEST['name'];
		$sort = $_REQUEST['sort'];
		$yunfare = $_REQUEST['yunfare'];
        $insertArr['mcount'] = $mcount;
		$insertArr['money'] = $money;
		$insertArr['name'] = $name;
		$insertArr['sort'] = $sort;
		$insertArr['yunfare'] = $yunfare;
        if ($_FILES) {

            $UpFileTool = new \Think\UpFileTool('images');// 实例化上传类

            $UpFileTool->upImage(0.3,'Uploads','Uploads');
            $images=$UpFileTool->srcImage;
            $sumbimages = $UpFileTool->sumbImage;
             
            $insertArr['sumbimage'] = $sumbimages;
			$insertArr['image'] = $images;
            
        }
        
		myinsert('buygroup',$insertArr);
        $returnArr = array(
			"code"=>200,
			"callbackType"=>"closeCurrent",
			"navTabId"=>"buygroup_list",
			"forwardUrl"=>"index.php?c=Tuangou&a=showList",
			"msg"=>"插入成功"
		);
		echo json_encode($returnArr);
    }
	
}